package test.org.nanotek.jpa.persistence.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class JPATestDAO {

	EntityManagerFactory emf;
	EntityManager em;
	
	@Before
	public void start()
	{ 
		emf = Persistence.createEntityManagerFactory("musicbrainz");
		em = emf.createEntityManager();
	}
	
	@Test
	public void test()
	{ 
		Metamodel metamodel = emf.getMetamodel();
		Set<EntityType<?>> entityTypes = metamodel.getEntities();
		ArrayList<String> entityNames = new ArrayList<String>();
		for (EntityType<?> entityType : entityTypes)
		{ 
			entityNames.add(entityType.getName());
		}
		Collections.sort(entityNames);
		for (String entityName:entityNames)
		{ 
			System.out.println(entityName);
		}
	}
	
	@After
	public void shutDown()
	{ 
		if (em !=null && em.isOpen()) 
			em.close();
		if (emf !=null && emf.isOpen()) 
			emf.close();
	}
	
}
